package controller;

/*
 * Action for researchers to input question for the questionnaire.
 * Used by both Binary and Network Binary type questions.
 */

import formbeans.ContinuousForm;
import helper.EntityAttributeType;
import helper.UserType;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.mybeans.dao.DAOException;
import org.mybeans.form.FormBeanException;
import org.mybeans.form.FormBeanFactory;

import databeans.Continuousq;
import databeans.Question;
import databeans.User;
import model.ContinuousqDAO;
import model.EntityClassDAO;
import model.Model;
import model.QuestionDAO;

public class ContinuousqAction extends Action {
	private FormBeanFactory<ContinuousForm> formBeanFactory = FormBeanFactory.getInstance(ContinuousForm.class);
	private ContinuousqDAO continuousqDAO;
	private QuestionDAO questionDAO;
	private EntityClassDAO ecDAO;
	
	public ContinuousqAction(Model model) {
		continuousqDAO = model.getContinuousqDAO();
		questionDAO = model.getQuestionDAO();
		ecDAO = model.getEntityClassDAO();
	}
	
    public String getName() {
		return "continuousq.do";
	}
    
    public String perform(HttpServletRequest request) {

        List<String> errors = new ArrayList<String>();
        request.setAttribute("errors",errors);
        
    	// First, validate whether researcher is accessing this
    	HttpSession session = request.getSession(false);
    	User user = (User) session.getAttribute("user");
    	
    	if (user.getType() != UserType.RESEARCHER) {
    		errors.add("You are not a researcher!");
    		return "homepage.jsp";
    	}
    	
    	// Validate the question number
    	Question question = (Question) session.getAttribute("question");
    	
    	if (question == null) {
    		errors.add("Invalid question number");
    		return "createsurvey.jsp";
    	}
    	
		String button = request.getParameter("button");
		
		if (button == null)
			return "continuousq.jsp";
		
		if (button.equals("Back")) {
			synchronized(session) {
				session.setAttribute("question", null);
			}
			return "createsurvey.jsp";
		}
		
		if (button.equals("Import Entity Data")) {
			synchronized(session) {
				session.setAttribute("pageFrom", "continuousq.jsp");
				session.setAttribute("importentitybody", "init");
			}
			return "importentity.jsp";
		}

		if (button.equals("Submit")) {
	        try {
        		ContinuousForm form = formBeanFactory.create(request);
            	request.setAttribute("continuousqform",form);
            	
            	if (!form.isPresent()) {
            		return "continuousq.jsp";
            	}
            	
        		// Validate for errors in the form
        		errors.addAll(form.checkContinuousFormErrors());
        		if (errors.size() != 0) {
        			return "continuousq.jsp";
        		}
        		
        		boolean network = Boolean.parseBoolean((String) session.getAttribute("network"));
        		
        		question.setQuestion(form.getQuestion());
        		question.setDynetmlAttr(form.getDynetmlAttr());
        		
        		if (network) { //Network Weighted Continuous Type Question
        			question.setEntityFrom("Respondents");
        			question.setEntityTo(form.getClassName());
        			question.setDynetmlType(-1);
        			questionDAO.updateEntityToFrom(question, ecDAO);
        		}
        		else //Continuous Type Question
        			question.setDynetmlType(EntityAttributeType.CATEGORY_NUMBER);
            	
        		questionDAO.updateQuestion(question);
        		synchronized(session) {
        			session.setAttribute("question", question);
        		}
        		
        		Continuousq continuousq = new Continuousq(question.getQid());
        		continuousq.setMin(new Integer(form.getMin()));
        		continuousq.setMax(new Integer(form.getMax()));
        		continuousq.setStep(new Integer(form.getStep()));
        		continuousqDAO.create(continuousq, questionDAO);
        		
        		Question[] questionlist = questionDAO.getQuestionBySidSortedQnum(question.getSid(), true);
    			synchronized(session) {
    				session.setAttribute("questionlist", questionlist);
    			}
        		return "surveysummary.jsp";
	        } catch (DAOException e) {
	        	errors.add(e.getMessage());
	        	return "errors.jsp";
	        } catch (FormBeanException e) {
	        	errors.add(e.getMessage());
	        	return "errors.jsp";
	        }
        }
        errors.add("Illegal button value of " + button);
		return "errors.jsp";
    }
}
